<?php
/**
 * Created by PhpStorm.
 * User: UI
 * Date: 2016/7/6
 * Time: 10:57
 */



define('PC_PATH',dirname(__FILE__).DIRECTORY_SEPARATOR);
define('CONFIG_PATH',ACMS_PATH.'acms'.DIRECTORY_SEPARATOR);
define('SITE_URL',isset($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:'');
AcmsBase::loadSystemFunction('global');
AcmsBase::loadSystemFunction('extention');
AcmsBase::autoLoadFunction();

class AcmsBase
{
    //appBegan
    public static function createApp(){
        return self::loadSystemClass('application');
    }

    public static function loadSystemClass($classname , $path = '',$initialize = 1){
        return self::loadClass($classname,$path,$initialize);
    }

    public static function loadAppClass(){

    }

    public static function loadModel(){

    }

    /**
     * @param $func
     * @return mixed
     */
    public static function loadSystemFunction($func){
        return self::_loadFunction($func);
    }

    /**
     * @param string $path
     */
    public static function autoLoadFunction($path=''){
        return self::_autoLoadFunction($path);
    }

    private static function _autoLoadFunction($path=''){
        if(empty($path)){
            $path = 'libs'.DIRECTORY_SEPARATOR.'functions'.DIRECTORY_SEPARATOR.'autoload';
        }
        $path .= DIRECTORY_SEPARATOR.'*.func.php';

        $auto_funcs = glob(PC_PATH.DIRECTORY_SEPARATOR.$path);
//        var_dump($auto_funcs);die;
        if(!empty($auto_funcs) && is_array($auto_funcs)){
            foreach($auto_funcs as $func_path){
                include $func_path;
            }
        }
    }

    /**
     * @param $func
     * @param string $path
     * @return bool
     */
    private static function _loadFunction($func,$path = ''){
        static $funcs = [];
        if(empty($path)){
            $path = 'libs'.DIRECTORY_SEPARATOR.'functions';
        }
        $path .= DIRECTORY_SEPARATOR.$func.'.func.php';/*var_dump(PC_PATH.$path);*/
        $key = md5($path);
        if(isset($funcs[$key])){
            return true;
        }
        if(file_exists(PC_PATH.$path)){
            include PC_PATH.$path;
        }else{
            $funcs[$key] = true;
            return true;
        }
    }

    /**
     * 加载类文件
     * @param $classname 类名
     * @param string $path 路径
     * @param int $initialize 是否初始化
     */
    private static function loadClass($classname , $path = '', $initialize = 1){
        static $classes = [];
        if(empty($path)){
            $path = 'libs'.DIRECTORY_SEPARATOR.'classes';
        }

        $key = md5($path.$classname);
        if(isset($classes[$key])){
            if(!empty($classes[$key])){
                return $classes[$key];
            }else{
                return true;
            }
        }
//var_dump(PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'class.php');die;
        if(file_exists(PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php')){
            include PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php';
            $name = $classname;
            if($myPath = self::myPath(PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'class.php')){
                include $myPath;
                $name = 'MY_'.$classname;
            }
            if($initialize){
                $classes[$key] = new $name;
            }else{
                $classes[$key] = true;
            }
            return $classes[$key];

        }else{
            return false;
        }

    }

    /**
     * 是否拥有自己的扩展文件
     * why?
     */
    public static function myPath($filepath){
        $path = pathinfo($filepath);
        if(file_exists($path['dirname'].DIRECTORY_SEPARATOR.'MY_'.$path['basename'])){
            return $path['dirname'].DIRECTORY_SEPARATOR.'MY_'.$path['basename'];
        }else{
            return false;
        }
    }

    /**
     * load configs
     * @param $file
     * @param string $key
     * @param string $default
     * @param bool $reload
     * @return string
     */
    public static function loadConfig($file,$key = '',$default='',$reload=false){
        static $configs = [];
        if(!$reload && isset($configs[$file])){
            if(empty($key)){
                return $configs[$file];
            }else if(isset($configs[$file][$key])){
                return $configs[$file][$key];
            }else {
                return $default;
            }
        }

        $path = CONFIG_PATH.'configs'.DIRECTORY_SEPARATOR.$file.'.php';
        if(file_exists($path)){
            $configs[$file] = include $path;
        }
        if(empty($key)){
            return $configs[$file];
        }else if(isset($configs[$file][$key])){
            return $configs[$file][$key];
        }else{
            return $default;
        }
    }



}